import { defineStore } from 'pinia'
import Cookies from 'js-cookie'

const SIDEBAR_STATUS_KEY = 'sidebar_status'
const SIDEBAR_STATUS_OPENED = 'opened'
const SIDEBAR_STATUS_CLOSED = 'closed'

export const useSidebarStore = defineStore('sidebar', {
  state: () => {
    const sidebarStatus = Cookies.get(SIDEBAR_STATUS_KEY)
    return {
      isCollapse: sidebarStatus === SIDEBAR_STATUS_CLOSED,
      withoutAnimation: false
    }
  },
  actions: {
    toggleSidebar() {
      this.isCollapse = !this.isCollapse
      this.withoutAnimation = false
      
      if (this.isCollapse) {
        Cookies.set(SIDEBAR_STATUS_KEY, SIDEBAR_STATUS_CLOSED)
      } else {
        Cookies.set(SIDEBAR_STATUS_KEY, SIDEBAR_STATUS_OPENED)
      }
    },
    closeSidebar(withoutAnimation = false) {
      this.isCollapse = true
      this.withoutAnimation = withoutAnimation
      Cookies.set(SIDEBAR_STATUS_KEY, SIDEBAR_STATUS_CLOSED)
    },
    openSidebar(withoutAnimation = false) {
      this.isCollapse = false
      this.withoutAnimation = withoutAnimation
      Cookies.set(SIDEBAR_STATUS_KEY, SIDEBAR_STATUS_OPENED)
    }
  }
}) 